﻿<?php
require('./database/db.php');
$mysql=new mysql();
error_reporting(0);
// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=10;
}
	//1是销量 2是价格
	$sort = $_REQUEST['sort'];
	//-1降序 1升序
	$sort_type = $_REQUEST['sort_type'];

	//默认按照最新商品显示
	$sortSql = 'order by goodsid desc';
	if($sort != '' && $sort_type != ''){
		$key = $sort == 1 ? 'xiaoliang':'price';
		$val = $sort_type == -1 ?'desc':'asc';
		$sortSql = ' order by '.$key.' '.$val.' ';
	}

// 分页 end
    $sql = "select * from goods  where tejia = 1  and goods.check = 1 ".$sortSql."  limit $de,$size";
	$goods = $mysql->getAll($sql);
	$len = count($goods);
	for($i=0;$i<$len;$i++){
		// 设置默认图
		$images = explode(',', $goods[$i]['images']);
		if ($images[$goods[$i]['defaultImgOrd']] !='') {
			$defaultImg = $images[$goods[$i]['defaultImgOrd']];
		}else{
			$defaultImg = $image[0];
		}
		$goods[$i]['mainimages'] = $defaultImg;
		// 设置默认图 end
		
		$goodsid =$goods[$i]['goodsid'];
		$sql2 = "select * from goodsselect where goodsid=$goodsid and number>0";
		$goods[$i]['goodsselect']=$mysql->getAll($sql2);

		$goods[$i]['number'] = $goods[$i]['xiaoliang'];
		$goods[$i]['name'] = $goods[$i]['title'];
		$goods[$i]['intro'] = $goods[$i]['summary'];
		$goods[$i]['id'] = $goods[$i]['goodsid'];
		$goods[$i]['content'] = 1;
		$goods[$i]['qq'] = $goods[$i]['qq'];
		$shopidsArray[]  = $goods[$i]['shopid'];

	}

	$arr = array('code' => 200, 'msg' =>'查询成功', 'shopids' =>implode(',',$shopidsArray) ,'goods' =>$goods);

echo json_encode($arr);
?>